{"componentChunkName":"component---src-templates-post-tsx","path":"/react-lifecycle/","result":{"data":{"markdownRemark":{"html":"<ul>\n<li>컴포넌트 초기 생성(Mount)</li>\n<li>컴포넌트 업데이트</li>\n<li>컴포넌트 제거(마운트 해제)</li>\n</ul>\n<hr>\n<h2 id=\"컴포넌트-초기-생성mount\" style=\"position:relative;\"><a href=\"#%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%B4%88%EA%B8%B0-%EC%83%9D%EC%84%B1mount\" aria-label=\"컴포넌트 초기 생성mount permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>컴포넌트 초기 생성(Mount)</h2>\n<p>컴포넌트가 브라우저에 나타나기 전, 후에 호출되는 API<br>\n아래의 순서대로 호출 된다.</p>\n<ul>\n<li><code class=\"language-text\">constructor()</code></li>\n<li><code class=\"language-text\">static getDerivedStateFromProps()</code></li>\n<li><code class=\"language-text\">render()</code></li>\n<li><code class=\"language-text\">componentDidMount()</code></li>\n</ul>\n<h3 id=\"1-constructor-\" style=\"position:relative;\"><a href=\"#1-constructor-\" aria-label=\"1 constructor  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. constructor( )</h3>\n<p>컴포넌트 생성자 함수, 컴포넌트가 새로 만들어질 때마다 이 함수가 호출된다.</p>\n<h3 id=\"2-static-getderivedstatefromprops-\" style=\"position:relative;\"><a href=\"#2-static-getderivedstatefromprops-\" aria-label=\"2 static getderivedstatefromprops  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. static getDerivedStateFromProps( )</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">static</span> <span class=\"token function\">getDerivedStateFromProps</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">prosp<span class=\"token punctuation\">,</span>state</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// 여기서는 setState 를 하는 것이 아니라</span>\n  <span class=\"token comment\">// 특정 props 가 바뀔 때 설정하고 설정하고 싶은 state 값을 리턴하는  형태로 사용된다.</span>\n  <span class=\"token comment\">/*\n  if (props.value !== state.value) {\n    return { value: props.value };\n  }\n  return null; // null 을 리턴하면 따로 업데이트 할 것은 없다라는 의미\n  */</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>최초 마운트 시와 업데이트 시 모두에서 render 메서드를 호출하기 직전에 호출된다.</p>\n<ul>\n<li>부모 컴포넌트가 다시 렌더링을 발생 시켰을 때에만 실행된다.</li>\n<li>해당 컴포넌트 내에서 지역적인 <code class=\"language-text\">setState()</code>가 발생한 경우에는 실행되지 않는다.</li>\n</ul>\n<h3 id=\"3-componentdidmount-\" style=\"position:relative;\"><a href=\"#3-componentdidmount-\" aria-label=\"3 componentdidmount  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. componentDidMount( )</h3>\n<p>컴포넌트가 마운트된 직후, 즉 컴포넌트가 화면에 나타나게 됐을 때 호출된다. 외부에서 데이터를 불러오기 위해, 네트워크 요청을 보내기 적절한 위치이다.</p>\n<ul>\n<li>외부 라이브러리 연동 : D3,</li>\n<li>컴포넌트에서 필요한 데이터 요청 : Ajax, GraphQL, etc</li>\n<li>DOM에 관련된 작업 : 스크롤 설정, 크기 읽어오기 등</li>\n</ul>\n<hr>\n<h2 id=\"컴포넌트-업데이트\" style=\"position:relative;\"><a href=\"#%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%97%85%EB%8D%B0%EC%9D%B4%ED%8A%B8\" aria-label=\"컴포넌트 업데이트 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>컴포넌트 업데이트</h2>\n<p>props 또는 state가 변경되어 컴포넌트가 다시 렌더링될 때, 아래의 순서대로 호출된다.</p>\n<ul>\n<li><code class=\"language-text\">static getDerivedStateFromProps()</code></li>\n<li><code class=\"language-text\">shouldComponentUpdate()</code></li>\n<li><code class=\"language-text\">render()</code></li>\n<li><code class=\"language-text\">getSnapshotBeforeUpdate()</code></li>\n<li><code class=\"language-text\">componentDidUpdate()</code></li>\n</ul>\n<h3 id=\"1-shouldcomponentupdate-\" style=\"position:relative;\"><a href=\"#1-shouldcomponentupdate-\" aria-label=\"1 shouldcomponentupdate  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. shouldComponentUpdate( )</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token function\">shouldComponentUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">nextProps<span class=\"token punctuation\">,</span> nextState</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// return false 하면 render()를 발생시키지 않는다.</span>\n  <span class=\"token comment\">// return this.props.checked !== nextProps.checked</span>\n  <span class=\"token keyword\">return</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>이 메서드는 오직 성능 최적화만을 위한 것이다.</p>\n<ul>\n<li><code class=\"language-text\">this.props</code>와 <code class=\"language-text\">nextProps</code>, 그리고 <code class=\"language-text\">this.state</code>와 <code class=\"language-text\">nextState</code>를 비교해 사용한다.</li>\n</ul>\n<h3 id=\"2-getsnapshotbeforeupdate-\" style=\"position:relative;\"><a href=\"#2-getsnapshotbeforeupdate-\" aria-label=\"2 getsnapshotbeforeupdate  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. getSnapshotBeforeUpdate( )</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token function\">getSnapshotBeforeUpdate</span><span class=\"token punctuation\">(</span>prevProps<span class=\"token punctuation\">,</span> prevState<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>가장 마지막으로 렌더링된 결과가 DOM 등에 반영되었을 때에 호출된다. 이 생명주기 메서드가 반환하는 값은 <code class=\"language-text\">componentDidUpdate()</code>에 인자로 전달된다.</p>\n<h3 id=\"3-componentdidupdate-\" style=\"position:relative;\"><a href=\"#3-componentdidupdate-\" aria-label=\"3 componentdidupdate  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. componentDidUpdate( )</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token function\">componentDidUpdate</span><span class=\"token punctuation\">(</span>prevProps<span class=\"token punctuation\">,</span> prevState<span class=\"token punctuation\">,</span> snapshot<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>이 메서드는 갱신이 일어난 직후에 호출된다.</p>\n<ul>\n<li>이전과 현재의 props를 비교해 네트워크 요청을 보내는 작업이 대부분 이 메서드에서 이루어진다.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token function\">componentDidUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">prevProps</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// 전형적인 사용 사례 (props 비교)</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>userID <span class=\"token operator\">!==</span> prevProps<span class=\"token punctuation\">.</span>userID<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">fetchData</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>userID<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<hr>\n<h2 id=\"컴포넌트-제거마운트-해제\" style=\"position:relative;\"><a href=\"#%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%A0%9C%EA%B1%B0%EB%A7%88%EC%9A%B4%ED%8A%B8-%ED%95%B4%EC%A0%9C\" aria-label=\"컴포넌트 제거마운트 해제 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>컴포넌트 제거(마운트 해제)</h2>\n<ul>\n<li><code class=\"language-text\">componentWillUnmount()</code></li>\n</ul>\n<h3 id=\"1-componentwillunmount-\" style=\"position:relative;\"><a href=\"#1-componentwillunmount-\" aria-label=\"1 componentwillunmount  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. componentWillUnmount( )</h3>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token function\">componentWillUnmount</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// 이벤트, setTimeout, 외부 라이브러리 인스턴스 제거</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>컴포넌트가 마운트 해제되어 제거되기 직전에 호출된다.</p>\n<ul>\n<li><code class=\"language-text\">componentDidMount()</code> 내에서 생성된 구독 해제</li>\n</ul>\n<hr>\n<h4 id=\"reference\" style=\"position:relative;\"><a href=\"#reference\" aria-label=\"reference permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reference</h4>\n<ul>\n<li><a href=\"https://velopert.com/1130\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://velopert.com/1130</a></li>\n<li><a href=\"https://ko.reactjs.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://ko.reactjs.org/</a></li>\n</ul>","excerpt":"컴포넌트 초기 생성(Mount) 컴포넌트 업데이트 컴포넌트 제거(마운트 해제) 컴포넌트 초기 생성(Mount) 컴포넌트가 브라우저에 나타나기 전, 후에 호출되는 API 아래의 순서대로 호출 된다.     1. constructor( ) 컴포넌트 생성…","tableOfContents":"<ul>\n<li>\n<p><a href=\"/react-lifecycle/#%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%B4%88%EA%B8%B0-%EC%83%9D%EC%84%B1mount\">컴포넌트 초기 생성(Mount)</a></p>\n<ul>\n<li><a href=\"/react-lifecycle/#1-constructor-\">1. constructor( )</a></li>\n<li><a href=\"/react-lifecycle/#2-static-getderivedstatefromprops-\">2. static getDerivedStateFromProps( )</a></li>\n<li><a href=\"/react-lifecycle/#3-componentdidmount-\">3. componentDidMount( )</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"/react-lifecycle/#%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%97%85%EB%8D%B0%EC%9D%B4%ED%8A%B8\">컴포넌트 업데이트</a></p>\n<ul>\n<li><a href=\"/react-lifecycle/#1-shouldcomponentupdate-\">1. shouldComponentUpdate( )</a></li>\n<li><a href=\"/react-lifecycle/#2-getsnapshotbeforeupdate-\">2. getSnapshotBeforeUpdate( )</a></li>\n<li><a href=\"/react-lifecycle/#3-componentdidupdate-\">3. componentDidUpdate( )</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"/react-lifecycle/#%EC%BB%B4%ED%8F%AC%EB%84%8C%ED%8A%B8-%EC%A0%9C%EA%B1%B0%EB%A7%88%EC%9A%B4%ED%8A%B8-%ED%95%B4%EC%A0%9C\">컴포넌트 제거(마운트 해제)</a></p>\n<ul>\n<li><a href=\"/react-lifecycle/#1-componentwillunmount-\">1. componentWillUnmount( )</a></li>\n</ul>\n</li>\n</ul>","fields":{"slug":"/react-lifecycle/"},"frontmatter":{"title":"React LifeCycle(리액트 생명주기)","date":"May 30, 2019","tags":["React"],"keywords":["react","리액트생명주기","componentlifecycle"],"update":"Jan 01, 0001"}}},"pageContext":{"slug":"/react-lifecycle/","series":[],"lastmod":"2019-05-30"}},"staticQueryHashes":["2027115977","694178885"]}